package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessesPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: byLengs
 * @Date: 2025/10/15 10:56
 * @Description:
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result cluesPoolList(BusinessesPoolDTO dto){
        PageResult<Business> result = businessService.businessesPoolList(dto);
        return Result.success(result);
    }

    @PutMapping("/back/{id}")
    public Result backBusinessPool(@PathVariable Integer id){
        businessService.backBusinessPool(id);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }

    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto) {
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id) {
        BusinessVO businessVO = businessService.getBusinessInfoById(id);
        return Result.success(businessVO);
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }


    @GetMapping
    public Result list(BusinessListDTO dto) {
        PageResult<BusinessDO> pageResult = businessService.businessList(dto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto) {
        businessService.addBusiness(dto);
        return Result.success();
    }
}
